package 第二章数据结构.单调栈.id830单调栈;

import java.util.Scanner;
import java.util.Stack;

/**
 * 单调栈：快速找到数组中每个元素左右 第一个比它大或者比它小的数
 */
public class Main {
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] nums = new int[n];
        for(int i = 0;i<n;i++){
            nums[i] = scanner.nextInt();
        }

        Stack<Integer> stack = new Stack<>();
        int[] ans = new int[n];
        for(int i = 0;i<n;i++){
            if(stack.isEmpty()){
                ans[i] = -1;
            }else {
                int cur = nums[i];
                while (!stack.isEmpty() && stack.peek() >= cur){
                    stack.pop();
                }
                if(stack.isEmpty()){
                    ans[i] = -1;
                }else ans[i] = stack.peek();
            }
            stack.push(nums[i]);
        }

        //处理输出
        for(int x : ans){
            System.out.print(x + " ");
        }
    }
}
